package cn.aj.system.service.impl;

import cn.aj.basic.service.impl.BaseServiceImpl;
import cn.aj.system.domain.Role;
import cn.aj.system.mapper.MenuMapper;
import cn.aj.system.mapper.PermissionMapper;
import cn.aj.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aj
 * @since 2022-11-29
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private MenuMapper menuMapper;

    /**
    * @Title: add
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/11/30 0030 23:43
    * @Parameters: [role]
    * @Return void
     * 角色权限菜单新增
    */
    @Override
    public void add(Role role) {
        super.add(role);
        // 新增权限
        permissionMapper.addRolePermission(role.getId(), role.getPermissions());
        // 新增菜单
        menuMapper.addRoleMenu(role.getId(), role.getMenus());
    }

    /**
    * @Title: update
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/11/30 0030 23:43
    * @Parameters: [role]
    * @Return void
     * 角色修改
    */
    @Override
    public void update(Role role) {
        super.update(role);
        permissionMapper.delRolePermission(role.getId());
        menuMapper.delRoleMenu(role.getId());
        // 新增权限
        permissionMapper.addRolePermission(role.getId(), role.getPermissions());
        // 新增菜单
        menuMapper.addRoleMenu(role.getId(), role.getMenus());
    }

    @Override
    public void delete(Long id) {
        super.delete(id);
        Role role = new Role();
        permissionMapper.delRolePermission(role.getId());
        menuMapper.delRoleMenu(role.getId());
    }
}
